1
การเปลี่ยนผ่านสู่การใช้งานจริง: ทัศนคติในการจัดวางระบบ
EvoClass-AI002Lecture 10
00:00

การเปลี่ยนผ่านสู่การใช้งานจริง: ทัศนคติในการจัดวางระบบ

โมดูลสุดท้ายนี้ช่วยปิดช่องว่างระหว่างงานวิจัยที่สำเร็จ — ซึ่งเราได้ผลการจำแนกที่แม่นยำสูงในเครื่องมือบันทึกข้อมูล — กับการทำงานที่เชื่อถือได้ การจัดวางระบบ (Deployment) คือกระบวนการสำคัญที่แปลงโมเดล PyTorch ให้กลายเป็นบริการขนาดเล็กที่มีความสมบูรณ์เอง และสามารถให้บริการผลลัพธ์ได้อย่างมีประสิทธิภาพแก่ผู้ใช้ปลายทาง โดยมีเวลาตอบสนองต่ำและ บริการที่มีความสมบูรณ์เอง ความสามารถในการให้บริการผลลัพธ์อย่างมีประสิทธิภาพแก่ผู้ใช้ปลายทางด้วยเวลาตอบสนองต่ำและ ความสามารถในการทำงานต่อเนื่องสูง

1. การเปลี่ยนทัศนคติสู่การใช้งานจริง

สภาพแวดล้อมทดลองของเครื่องมือบันทึกข้อมูล (Jupyter notebook) มีลักษณะเก็บสถานะและไม่แข็งแรงพอสำหรับการใช้งานจริง เราต้องปรับโครงสร้างโค้ดจากแบบการเขียนโปรแกรมทดลองมาเป็นส่วนประกอบที่มีโครงสร้างและแยกส่วน ซึ่งเหมาะสมกับการรับคำขอพร้อมกัน ลดการใช้ทรัพยากร และรวมเข้ากับระบบที่ใหญ่กว่าได้อย่างราบรื่น

การประมวลผลด้วยเวลาตอบสนองต่ำ: การทำให้เวลาการคาดการณ์คงที่ต่ำกว่าเกณฑ์ที่ตั้งไว้ (ตัวอย่างเช่น $50\text{ms}$) ซึ่งเป็นสิ่งสำคัญสำหรับแอปพลิเคชันที่ต้องการผลลัพธ์ทันที
ความสามารถในการทำงานต่อเนื่องสูง: การออกแบบบริการให้มีความน่าเชื่อถือ ไม่มีสถานะ และสามารถกลับมาทำงานได้รวดเร็วหลังเกิดข้อผิดพลาด
ความสามารถในการทำซ้ำได้: การประกันว่าโมเดลที่จัดวางและสภาพแวดล้อม (ขึ้นกับการพึ่งพา น้ำหนัก ค่าตั้งค่า) ตรงกับผลการตรวจสอบจากการวิจัยอย่างแม่นยำ
โฟกัส: บริการโมเดล
แทนที่จะจัดวางสคริปต์ฝึกอบรมทั้งหมด เราจะจัดวางแค่เวอร์ชันที่มีขนาดเล็กและมีความสมบูรณ์เอง ซึ่งต้องจัดการเพียงสามงานเท่านั้น: โหลดอาร์ติแฟกต์ของโมเดลที่ได้รับการปรับแต่ง ประมวลผลข้อมูลนำเข้า และดำเนินการคำนวณไปข้างหน้าเพื่อส่งผลลัพธ์กลับมา
inference_service.py
TERMINALbash — uvicorn-service
> Ready. Click "Simulate Deployment Flow" to run.
>
ARTIFACT INSPECTOR Live

Simulate flow to view loaded production artifacts.
Question 1
Which feature of a Jupyter notebook makes it unsuitable for production deployment?
It primarily uses Python code
It is inherently stateful and resource-intensive
It cannot directly access the GPU
Question 2
What is the primary purpose of converting a PyTorch model to TorchScript or ONNX before deployment?
Optimization for faster C++ execution and reduced Python dependency
To prevent model theft or reverse engineering
To automatically handle input data preprocessing
Question 3
When designing a production API, when should the model weights be loaded?
Once, when the service initializes
At the start of every prediction request
When the first request to the service is received
Challenge: Defining the Minimal Service
Plan the structural requirements for a low-latency service.
You need to deploy a complex image classification model ($1\text{GB}$) that requires specialized image preprocessing. It must handle $50$ requests per second.
Step 1
To ensure high throughput and low average latency, what is the single most critical structural change needed for the Python script?
Solution:
Refactor the codebase into isolated modules (Preprocessing, Model Definition, Inference Runner) and ensure the entire process is packaged for containerization.
Step 2
What is the minimum necessary "artifact" to ship, besides the trained weights?
Solution:
The exact code/class definition used for preprocessing and the model architecture definition, serialized and coupled with the weights.